﻿namespace com.stifel.BranchMgmtDao
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net.Mail;

    using com.stifel.Extensions.DateTimeExtensions;
    using com.stifel.Extensions.DoubleExtensions;
    using com.stifel.SharePoint;

    using Microsoft.SharePoint;

    partial class StifelBranchConfiguration
    {
        #region Fields

        const string BRANCH = "@branch";
        const string ITEMTITLE = "@itemtitle";
        const string ITEMURL = "@itemurl";

        DateTime? _RepeatBranchTelMaintenanceContractExpirationNotificationDate;
        DateTime? _RepeatBranchUPSScheduledMaintenanceNotificationDate;
        int? _RepeatNotificationEveryNDaysUntilBranchLeaseResolutionAsInteger;
        DateTime? _TargetBranchLeaseExpirationDate;
        DateTime? _TargetBranchTelMaintenanceContractExpirationDate;
        DateTime? _TargetBranchUPSScheduledMaintenanceDate;

        #endregion Fields

        #region Enumerations

        public enum Area : int
        {
            Hardware,
            Lease,
            Telephone
        }

        #endregion Enumerations

        #region Properties

        public string ProductionSiteURLForTheLiveBranchesDescription
        {
            get
            {
                string[ ] s = ProductionSiteURLForTheLiveBranches.Split( ',' );
                if ( s.Length >= 2 )
                    return s[ 1 ].Trim( );
                else
                    return string.Empty;
            }
        }

        public string ProductionSiteURLForTheLiveBranchesUrl
        {
            get
            {
                string[ ] s = ProductionSiteURLForTheLiveBranches.Split( ',' );
                if ( s.Length >= 1 )
                    return s[ 0 ].Trim( );
                else
                    return string.Empty;
            }
        }

        public int RepeatNotificationEveryNDaysUntilBranchLeaseResolutionAsInteger
        {
            get
            {
                if ( !_RepeatNotificationEveryNDaysUntilBranchLeaseResolutionAsInteger.HasValue )
                    _RepeatNotificationEveryNDaysUntilBranchLeaseResolutionAsInteger = RepeatNotificationEveryNDaysUntilBranchLeaseResolution.HasValue ? RepeatNotificationEveryNDaysUntilBranchLeaseResolution.Value.ToInt32( ) : 5;
                return _RepeatNotificationEveryNDaysUntilBranchLeaseResolutionAsInteger.Value;
            }
        }

        //public DateTime RepeatBranchLeaseExpirationNotificationDate
        //{
        //    get
        //    {
        //        if ( !_RepeatBranchLeaseExpirationNotificationDate.HasValue )
        //        {
        //            double? d = RepeatNotificationEveryNDaysUntilBranchLeaseResolution;
        //            int i = d.HasValue ? d.Value.ToInt32( ) : 15;
        //            _RepeatBranchLeaseExpirationNotificationDate = DateTime.Now.SubtractDays( i );
        //        }
        //        return _RepeatBranchLeaseExpirationNotificationDate.Value;
        //    }
        //}
        //public DateTime RepeatBranchTelMaintenanceContractExpirationNotificationDate
        //{
        //    get
        //    {
        //        if ( !_RepeatBranchTelMaintenanceContractExpirationNotificationDate.HasValue )
        //        {
        //            double? d = RepeatNotificationEveryNDaysUntilBranchTelMaintenanceContractResolution;
        //            int i = d.HasValue ? d.Value.ToInt32( ) : 15;
        //            _RepeatBranchTelMaintenanceContractExpirationNotificationDate = DateTime.Now.SubtractDays( i );
        //        }
        //        return _RepeatBranchTelMaintenanceContractExpirationNotificationDate.Value;
        //    }
        //}
        //public DateTime RepeatBranchUPSScheduledMaintenanceNotificationDate
        //{
        //    get
        //    {
        //        if ( !_RepeatBranchUPSScheduledMaintenanceNotificationDate.HasValue )
        //        {
        //            double? d = RepeatNotificationEveryNDaysUntilScheduleBranchHardwareMaintenanceCompleted;
        //            int i = d.HasValue ? d.Value.ToInt32( ) : 15;
        //            _RepeatBranchUPSScheduledMaintenanceNotificationDate = DateTime.Now.SubtractDays( i );
        //        }
        //        return _RepeatBranchUPSScheduledMaintenanceNotificationDate.Value;
        //    }
        //}
        public string SiteURLForTheBranchHardwareMaintenanceNotificationDescription
        {
            get
            {
                string[ ] s = SiteURLForTheBranchHardwareMaintenanceNotification.Split( ',' );
                if ( s.Length >= 2 )
                    return s[ 1 ].Trim( );
                else
                    return string.Empty;
            }
        }

        public string SiteURLForTheBranchHardwareMaintenanceNotificationUrl
        {
            get
            {
                string[ ] s = SiteURLForTheBranchHardwareMaintenanceNotification.Split( ',' );
                if ( s.Length >= 1 )
                    return s[ 0 ].Trim( );
                else
                    return string.Empty;
            }
        }

        public string SiteURLForTheBranchLeaseNotificationDescription
        {
            get
            {
                string[ ] s = SiteURLForTheBranchLeaseNotification.Split( ',' );
                if ( s.Length >= 2 )
                    return s[ 1 ].Trim( );
                else
                    return string.Empty;
            }
        }

        public string SiteURLForTheBranchLeaseNotificationUrl
        {
            get
            {
                string[ ] s = SiteURLForTheBranchLeaseNotification.Split( ',' );
                if ( s.Length >= 1 )
                    return s[ 0 ].Trim( );
                else
                    return string.Empty;
            }
        }

        public string SiteURLForTheBranchTelMaintenanceContractNotificationDescription
        {
            get
            {
                string[ ] s = SiteURLForTheBranchTelMaintenanceContractNotification.Split( ',' );
                if ( s.Length >= 2 )
                    return s[ 1 ].Trim( );
                else
                    return string.Empty;
            }
        }

        public string SiteURLForTheBranchTelMaintenanceContractNotificationUrl
        {
            get
            {
                string[ ] s = SiteURLForTheBranchTelMaintenanceContractNotification.Split( ',' );
                if ( s.Length >= 1 )
                    return s[ 0 ].Trim( );
                else
                    return string.Empty;
            }
        }

        public DateTime TargetBranchLeaseExpirationDate
        {
            get
            {
                if ( !_TargetBranchLeaseExpirationDate.HasValue )
                {
                    double? d = BeginNotificationNDaysBeforeBranchLeaseExpiration;
                    int i = d.HasValue ? d.Value.ToInt32( ) : 90;

                    _TargetBranchLeaseExpirationDate = DateTime.Now.AddDays( i );
                }
                return _TargetBranchLeaseExpirationDate.Value;
            }
        }

        public DateTime TargetBranchTelMaintenanceContractExpirationDate
        {
            get
            {
                if ( !_TargetBranchTelMaintenanceContractExpirationDate.HasValue )
                {
                    double? d = BeginNotificationNDaysBeforeBranchTelMaintenanceContractExpiration;
                    int i = d.HasValue ? d.Value.ToInt32( ) : 90;

                    _TargetBranchTelMaintenanceContractExpirationDate = DateTime.Now.SubtractDays( i );
                }
                return _TargetBranchTelMaintenanceContractExpirationDate.Value;
            }
        }

        public DateTime TargetBranchUPSScheduledMaintenanceDate
        {
            get
            {
                if ( !_TargetBranchUPSScheduledMaintenanceDate.HasValue )
                {
                    double? d = BeginNotificationNDaysBeforeBranchHardwareScheduledMaintenance;
                    int i = d.HasValue ? d.Value.ToInt32( ) : 90;

                    _TargetBranchUPSScheduledMaintenanceDate = DateTime.Now.SubtractDays( i );
                }
                return _TargetBranchUPSScheduledMaintenanceDate.Value;
            }
        }

        #endregion Properties

        #region Methods

        public string FormatNotification( Area area, string itemUrl, string itemTitle, string branch )
        {
            string s = string.Empty;
            switch ( area )
            {
                case Area.Hardware:
                    s = formatNotification( BranchHardwareMaintenanceScheduledNotificationTemplate, itemUrl, itemTitle, branch );
                    break;
                case Area.Lease:
                    s = formatNotification( BranchLeaseExpirationNotificationTemplate, itemUrl, itemTitle, branch );
                    break;
                case Area.Telephone:
                    s = formatNotification( BranchTelMaintenanceContractExpirationNotificationTemplate, itemUrl, itemTitle, branch );
                    break;
                default:
                    break;
            }
            return s;
        }

        public string FormatNotificationTitle( Area area, string itemUrl, string itemTitle, string branch )
        {
            string s = string.Empty;
            switch ( area )
            {
                case Area.Hardware:
                    s = formatNotificationTitle( BranchHardwareMaintenanceScheduledNotificationTitle, itemUrl, itemTitle, branch );
                    break;
                case Area.Lease:
                    s = formatNotificationTitle( BranchLeaseExpirationNotificationTitle, itemUrl, itemTitle, branch );
                    break;
                case Area.Telephone:
                    s = formatNotificationTitle( BranchTelMaintenanceContractExpirationNotificationTitle, itemUrl, itemTitle, branch );
                    break;
                default:
                    break;
            }
            return s;
        }

        public string FormatPageUrl( string itemUrl, string itemTitle )
        {
            return string.Format( "{0}, {1}", itemUrl, itemTitle );
        }

        public List<MailAddress> GetSecurityGroupEmailAddresses( Area area, string siteUrl )
        {
            List<MailAddress> addresses = new List<MailAddress>( );
            switch ( area )
            {
                case Area.Hardware:
                    addresses = getSecurityGroupEmailAddresses( siteUrl, BranchHardwareMaintenanceGroupImnName );
                    break;
                case Area.Lease:
                    addresses = getSecurityGroupEmailAddresses( siteUrl, BranchLeaseGroupImnName );
                    break;
                case Area.Telephone:
                    addresses = getSecurityGroupEmailAddresses( siteUrl, BranchTelMaintenanceContractGroupImnName );
                    break;
                default:
                    break;
            }
            return addresses;
        }

        string formatNotification( string template, string itemUrl, string itemTitle, string branch )
        {
            return template.Replace( BRANCH, branch ).Replace( ITEMTITLE, itemTitle ).Replace( ITEMURL, string.Format( "<a href=\"{0}\">{1}</a>", itemUrl, itemTitle ) );
        }

        string formatNotificationTitle( string title, string itemUrl, string itemTitle, string branch )
        {
            return title.Replace( BRANCH, branch ).Replace( ITEMTITLE, itemTitle ).Replace( ITEMURL, string.Format( "<a href=\"{0}\">{1}</a>", itemUrl, itemTitle ) );
        }

        List<MailAddress> getSecurityGroupEmailAddresses( string siteUrl, string groupName )
        {
            List<MailAddress> addresses = new List<MailAddress>( );
            if ( !string.IsNullOrEmpty( groupName ) )
            {
                List<SPUserInfoExpanded> users = getSecurityGroupUsers( siteUrl, groupName );
                if ( users.Count > 0 )
                    users.ForEach( a => addresses.Add( new MailAddress( a.Email, string.IsNullOrEmpty( a.Name ) ? a.LoginName : a.Name ) ) );
            }
            return addresses;
        }

        List<SPUserInfoExpanded> getSecurityGroupUsers( string siteUrl, string groupName )
        {
            List<SPUserInfoExpanded> users = new List<SPUserInfoExpanded>( );
            if ( !string.IsNullOrEmpty( groupName ) )
            {
                using ( SPSite spSite = new SPSite( siteUrl ) )
                {
                    using ( SPWeb spWeb = spSite.OpenWeb( ) )
                    {
                        IEnumerable<SPGroup> g = spWeb.SiteGroups.Cast<SPGroup>( );
                        SPGroup group = g.First( a => a.Name.Equals( groupName, StringComparison.OrdinalIgnoreCase ) );
                        if ( group != null )
                        {
                            foreach ( SPUser u in group.Users )
                            {
                                if ( u != null )
                                    users.Add( new SPUserInfoExpanded
                                    {
                                        Email = u.Email,
                                        Id = u.ID,
                                        LoginName = u.LoginName,
                                        Name = u.Name,
                                        Notes = u.Notes,
                                        Sid = u.Sid
                                    } );
                            }
                        }
                    }
                }
            }
            return users;
        }

        #endregion Methods
    }
}